package cn.jinhe.doc.view.action;

import java.util.HashSet;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.jinhe.doc.base.BaseAction;
import cn.jinhe.doc.domain.Department;
import cn.jinhe.doc.domain.Role;
import cn.jinhe.doc.domain.User;
import cn.jinhe.doc.util.DepartmentUtils;
import cn.jinhe.doc.util.QueryHelper;

@Controller
@Scope("prototype")
public class UserAction extends BaseAction<User> {

	
	private Long departmentId;
	private Long[] roleIds;


	/*
	 * 列表
	 */
	public String list() throws Exception {
		//List<User> userList = userService.findAll();
		//ActionContext.getContext().put("userList", userList);
		
		//准备分页信息
		new QueryHelper(User.class, "u").preparePageBean(userService, pageNum, pageSize);
		
		return "list";
	}

	/*
	 * 删除
	 */
	public String delete() throws Exception {
		userService.delete(model.getId());
		return "toList";
	}

	/*
	 * 添加
	 */
	public String add() throws Exception {
		
		model.setDepartment(departmentService.getById(departmentId));
		List<Role> roleList=roleService.getByIds(roleIds);
		model.setRoles(new HashSet<Role>(roleList));
//		model.setRoles(new ArrayList().addAll(roleList));
		
		String md5Digest=DigestUtils.md5Hex("1234");
		model.setPassword(md5Digest);
		userService.save(model);

		return "toList";
	}

	/*
	 * 添加页面
	 */
	public String addUI() throws Exception {
		List<Department> topList = departmentService.findTopList();
		List<Department> departmentList = DepartmentUtils
				.getAllDepartments(topList);

		ActionContext.getContext().put("departmentList", departmentList);
		
		
		List<Role> roleList = roleService.findAll();
		ActionContext.getContext().put("roleList", roleList);
		return "saveUI";
	}

	/*
	 * 修改页面
	 */
	public String editUI() throws Exception {
		List<Department> topList = departmentService.findTopList();
		List<Department> departmentList = DepartmentUtils
				.getAllDepartments(topList);

		ActionContext.getContext().put("departmentList", departmentList);
		
		
		List<Role> roleList = roleService.findAll();
		ActionContext.getContext().put("roleList", roleList);
		
		//准备回显数据
		User user=userService.getById(model.getId());
		ActionContext.getContext().getValueStack().push(user);
		if(user.getDepartment()!=null){
		departmentId=user.getDepartment().getId();
		}
		if(user.getRoles()!=null){
			roleIds=new Long[user.getRoles().size()];
			int index=0;
			for(Role role:user.getRoles())
			{
				roleIds[index]=role.getId();						
			}
		}
		return "saveUI";
	}

	/*
	 * 修改
	 */
	public String edit() throws Exception {
		//1,从数据库中取出原对象
		User user=userService.getById(model.getId());
		
		//2，设置要修改的属性
		user.setLoginName(model.getLoginName());
		user.setName(model.getName());
		user.setGender(model.getGender());
		user.setPhoneNumber(model.getPhoneNumber());
		user.setEmail(model.getEmail());
		user.setDescription(model.getDescription());
		// >> 设置所属部门
		user.setDepartment(departmentService.getById(departmentId));
		// >> 设置关联的岗位
		List<Role> roleList = roleService.getByIds(roleIds);
		user.setRoles(new HashSet<Role>(roleList));
		//3，更新到数据库
		userService.update(user);
		
		return "toList";
	}

	/*
	 * 初始化密碼
	 */
	public String initPassword() throws Exception {
		User user=userService.getById(model.getId());

		String md5Digest=DigestUtils.md5Hex("1234");
		user.setPassword(md5Digest);
		userService.update(user);
		
		return "toList";
	}
	
	/**
	 * 登录页面
	 * @return
	 */
	public String loginUI() throws Exception{
			
		return "loginUI";
	}
	
	/**
	 * 登录
	 * @return
	 */
	public String login() throws Exception{
		User user = userService.findByLoginNameAndPassword(model.getLoginName(), model.getPassword());
		if (user == null) {
			addFieldError("login", "用户名或密码不正确！");
			return "loginUI";
		} else {
			// 登录用户
			ActionContext.getContext().getSession().put("user", user);
			return "toIndex";
		}	
	}
	
	/**
	 * 注销
	 * @return
	 */
	public String logout() throws Exception{
		ActionContext.getContext().getSession().remove("user");
		return "logout";		
	}
	public Long getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(Long departmentId) {
		this.departmentId = departmentId;
	}
	public Long[] getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(Long[] roleIds) {
		this.roleIds = roleIds;
	}
}
